/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-27
 * Time: 16:49
 */

//单链表
public class MySignalList<T> {

    static class ListNode {
        public Object val;
        public ListNode next;

        public ListNode(Object val) {
            this.val = val;
        }
    }

    public ListNode head;

    //打印
    public void disPlay() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //申请结点
    public ListNode buyNode(T val) {
        return new ListNode(val);
    }

    //头插
    public void addFirst(T val) {
        ListNode newNode = buyNode(val);
        newNode.next = head;
        head = newNode;
    }

    //尾插
    public void addLast(T val) {
        ListNode newNode = buyNode(val);
        if (head == null) {
            head = newNode;
            return;
        }
        //找尾
        ListNode tail = head;
        while (tail.next != null) {
            tail = tail.next;
        }
        tail.next = newNode;
    }

    //在Index位置插入
    public void addIndex(int Index, T val) {
        //判断Index的合法性
        try {
            if (Index < 0 || Index > size()) {
                throw new IndexNotLegalException("Index不合法");
            }
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        if (Index == 0) {
            addFirst(val);
            return;
        }
        if (Index == size()) {
            addLast(val);
            return;
        }
        ListNode newNode = buyNode(val);
        ListNode cur = head;
        for (int i = 0; i < Index - 1; i++) {
            cur = cur.next;
        }
        //cur在index前面
        newNode.next = cur.next;
        cur.next = newNode;
    }

    //删除第一次出现的key
    public void remove(T key) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        ListNode prev = null;
        while (cur != null) {
            if (cur.val.equals(key)) {
                if (prev == null) {
                    head = head.next;
                    return;
                }
                prev.next = cur.next;
                return;
            }
            prev = cur;
            cur = cur.next;
        }
    }

    //删除所有的key
    public void removeAll(T key) {
        if (head == null) {
            return;
        }
        //如果前半部分都是key
        while (head.val.equals(key)) {
            head = head.next;
            if (head == null) {
                return;
            }
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if (cur.val.equals(key)) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
    }

    //是否包含val
    public boolean contains(T val) {
        if (head == null) {
            return false;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val.equals(val)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //返回结点
    public ListNode find(T findVal) {
        if (head == null) {
            return null;
        }
        ListNode ret = head;
        while (ret != null) {
            if (ret.val.equals(findVal)) {
                return ret;
            }
            ret = ret.next;
        }
        return null;
    }

    //修改第一次出现的oldVal,将它改为newVal
    public void modify(T oldVal, T newVal) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val.equals(oldVal)) {
                cur.val = newVal;
                return;
            }
            cur = cur.next;
        }
    }

    //修改所有的的oldVal,将它改为newVal
    public void modifyAll(T oldVal, T newVal) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val.equals(oldVal)) {
                cur.val = newVal;
            }
            cur = cur.next;
        }
    }

    public int size() {
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            cur = cur.next;
            size++;
        }
        return size;
    }
}
